﻿using UnityEngine;
using System.Collections;
using System.Text;
using System.IO;  

public class MenuEntry : MonoBehaviour {
	//Public Variables
	public bool isNewGame;
	public bool isLoadGame;
	public bool isDifficulty;
	public bool isExit;

	public bool isStandard;
	public bool isElite;

	public TextMesh currentDifficultyLabel;

	public AudioClip mouseHover;
	public AudioClip mouseSelect;

	//Private Variables
	private string subPath ="GameFiles";
	private string txtFileName = "stats.txt";

	void Start(){
		//Check if GameFiles folder exists
		bool exists = System.IO.Directory.Exists(subPath);
					//If:
					//A: Does NOT exist:
		if(!exists){	
					//Create folder Gamefiles with txt file called stats
			System.IO.Directory.CreateDirectory(subPath);
				//Write difficulty 'Standard' and level 1 to txt file for load purpose
			System.IO.File.WriteAllText(subPath + "/" + txtFileName, "Standard\n1");
		}else{
			string[] lines = File.ReadAllLines(subPath + "/" + txtFileName);
			currentDifficultyLabel.text = "Current Difficulty: " + lines[0];

		}
	}

	void OnMouseUp(){
		audio.PlayOneShot(mouseSelect);

		if(isNewGame){
					//Read Text File Called stats.txt in GameFiles folder
			StreamReader theReader = new StreamReader(subPath + "/" + txtFileName, Encoding.Default);
			string readDif = theReader.ReadLine();
			Application.LoadLevel("Level 1 - " + readDif);
			theReader.Close();

				//Erase current progress and start new level accordingly to the set difficulty
			System.IO.File.WriteAllText(subPath + "/" + txtFileName, readDif + "\n1");

			GameStatus.currentLevel = "1";
			GameStatus.difficulty = readDif;
			

		}
		

		if(isLoadGame){
				//Read the stats from the text file
			StreamReader theReader = new StreamReader(subPath + "/" + txtFileName, Encoding.Default);
			string readDif = theReader.ReadLine();
			string readCurrentLevel = theReader.ReadLine();
				//Load level based on lines in text file 
			Application.LoadLevel("Level " + readCurrentLevel + " - " + readDif);

			GameStatus.currentLevel = readCurrentLevel;
			GameStatus.difficulty = readDif;

		}

		if(isDifficulty){
			MenuControllerScript.DifficultySelect(true);
		}

		if(isExit){
			print("Exit Pressed");
			Application.Quit();
		}


			//Code for switchin Difficulty
		if(isStandard){
			string[] lines = File.ReadAllLines(subPath + "/" + txtFileName);
			lines[0] = "Standard"; 
			File.WriteAllLines(subPath + "/" + txtFileName, lines);
			MenuControllerScript.DifficultySelect(false);
			currentDifficultyLabel.text = "Current Difficulty: Standard";
		}

		if(isElite){
			string[] lines = File.ReadAllLines(subPath + "/" + txtFileName);
			lines[0] = "Elite"; 
			File.WriteAllLines(subPath + "/" + txtFileName, lines);
			MenuControllerScript.DifficultySelect(false);
			currentDifficultyLabel.text = "Current Difficulty: Elite";

		}
	}

	void OnMouseEnter(){

		//Player sound
		audio.PlayOneShot(mouseHover);
//change text color
		renderer.material.color=Color.green;
	}

	void OnMouseExit(){
//change text color
		renderer.material.color=Color.white;
	}
}
